package soulkun.library.utils

import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.content.Intent
import android.content.res.Resources
import android.graphics.Rect
import android.os.Bundle
import android.view.TouchDelegate
import android.view.View
import android.view.ViewGroup
import androidx.annotation.IdRes
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.navigation.NavController
import androidx.navigation.NavOptions
import androidx.navigation.Navigator
import com.google.android.material.bottomnavigation.BottomNavigationView
import soulkun.library.LibraryInit
import soulkun.library.utils.log.LogUtils
import soulkun.library.utils.screen.DensityUtils
import soulkun.library.utils.toast.ToastUtils
import java.util.ArrayList
import kotlin.random.Random

fun getAppContext(): Context = LibraryInit.context

fun getAppResources(): Resources = getAppContext().resources

fun Int.toPx() =
    DensityUtils.dp2px(this.toFloat())

fun Int.toPxF() =
    DensityUtils.dp2pxF(this.toFloat())

fun Float.toPx() =
    DensityUtils.dp2px(this)

fun Float.toPxF() =
    DensityUtils.dp2pxF(this)

fun Double.percent(): Boolean {
    return Random.nextDouble() <= this
}

fun <T> T.println() {
    println(this)
}

fun <T> T.toast() {
    ToastUtils.show(this.toString())
}

fun <T> T.logI() {
    LogUtils.logI(this.toString())
}

fun <T> T.logE() {
    LogUtils.logE(this.toString())
}

fun Fragment.startActivity(clazz: Class<out Activity>) {
    val intent = Intent(this.context, clazz)
    startActivity(intent)
}

fun Activity.startActivity(clazz: Class<out Activity>) {
    val intent = Intent(this, clazz)
    startActivity(intent)
}

fun View.visible(b: Boolean) {
    visibility = if (b) View.VISIBLE else View.GONE
}

fun View.invisible(b: Boolean) {
    visibility = if (b) View.INVISIBLE else View.VISIBLE
}

fun View.expandTouchArea(size: Int) {
    (parent as View).post {
        val rect = Rect()
        getHitRect(rect)
        rect.top -= size
        rect.bottom += size
        rect.left -= size
        rect.right += size
        (parent as View).touchDelegate = TouchDelegate(rect, this)
    }
}

fun View.expandTouchArea(top: Int, bottom: Int, left: Int, right: Int) {
    (parent as View).post {
        val rect = Rect()
        getHitRect(rect)
        rect.top -= top
        rect.bottom += bottom
        rect.left -= left
        rect.right += right
        (parent as View).touchDelegate = TouchDelegate(rect, this)
    }
}

/**
 * true显示 false消失
 */
fun Dialog.showOrDismiss(b: Boolean) {
    if (!b && isShowing) dismiss()
    else if (b && !isShowing) show()
    else return
}

/**
 * auto显示和消失
 */
fun Dialog.autoShow() {
    if (isShowing) dismiss()
    else if (!isShowing) show()
    else return
}

/**
 * Navigation安全导航，防止重复点击崩溃
 */
fun NavController.navigateSafe(
    @IdRes resId: Int,
    @IdRes currentId: Int,
    args: Bundle? = null,
    navOptions: NavOptions? = null,
    navExtras: Navigator.Extras? = null
) {
    val action = currentDestination?.getAction(resId) ?: graph.getAction(resId)
    if (action != null && currentDestination?.id == currentId) {
        navigate(resId, args, navOptions, navExtras)
    }
}

/**
 * 清除BottomNavigationView长按时的默认toast
 * @param ids 为BottomNavigationView菜单中的Item的Id，如R.id.xxx
 */
fun BottomNavigationView.clearToast(ids: MutableList<Int>) {
    val bottomNavigationMenuView = this.getChildAt(0) as ViewGroup
    //遍历子View,重写长按点击事件
    for (position in 0 until ids.size) {
        bottomNavigationMenuView.getChildAt(position)
            .findViewById<View>((ids[position] as Int?)!!)
            .setOnLongClickListener { true }
    }
}

